package odfconverter;

import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.URIResolver;
import javax.xml.transform.stream.StreamSource;

import org.apache.tools.zip.ZipFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipException;


public class ZipURIResolver implements URIResolver {
	private ZipFile zipFile;

	public ZipURIResolver(String zipFileName) {
		try {
			this.zipFile = new ZipFile(zipFileName);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public Source resolve(String href, String base) throws TransformerException {
		// TODO Auto-generated method stub
	    try {
	    	//ZipFile zipFile = new ZipFile("b.odp");
	    	InputStream inStream = zipFile.getInputStream(zipFile.getEntry(href));
	    	return new StreamSource(inStream);
	      }
	      catch (Exception e) {
	    	  //e.printStackTrace();
	    	  //System.out.println(href);
	      }		
		return null;
	}
	
	public InputStream GetEntry(String entryName)
	{
		try {
			return zipFile.getInputStream(zipFile.getEntry(entryName));
		} catch (ZipException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
